module AboutYou
  module SDK
    module Model
      ###
      # This Class represents an autocomplete model
      ###
      class Autocomplete
        # this constant is used for values which are not requested
        NOT_REQUESTED = nil
        # argument for api when requiring products
        TYPE_PRODUCTS = 'products'
        # argument for api when requiring categories
        TYPE_CATEGORIES = 'categories'
        # argument for api when requiring brands
        TYPE_BRANDS = 'brands'

        # the products of the autocomplete
        attr_accessor :products
        # the categories of the autocomplete
        attr_accessor :categories
        # the brands of the autocomplete
        attr_accessor :brands

        ###
        # the Constructor for the autocomplete class
        #
        # * *Args*    :
        #   - +categories+ -> the products of the autocomplete
        #   - +products+ -> the categories of the autocomplete
        #   - +brands+ -> the brands of the autocomplete
        #
        # * *Returns* :
        #   - Instance of AboutYou::SDK::Model::AutoComplete
        ###
        def initialize(categories = nil, products = nil, brands = nil)
          self.categories = categories
          self.products   = products
          self.brands     = brands
        end

        ###
        # This method lets you build an autocomplete model by a json response from the api
        #
        # * *Args*    :
        #   - +json_object+ -> the json response from the api
        #   - +factory+ -> the model factory responsible for building the models
        #
        # * *Returns* :
        #   - Instance of AboutYou::SDK::Model::AutoComplete
        ###
        def self.create_from_json(json_object, factory)
          new(
            parse_categories(json_object, factory),
            parse_products(json_object, factory),
            parse_brands(json_object, factory)
          )
        end

        ###
        # This method parses the json object and builds category models from it
        #
        # * *Args*    :
        #   - +json_object+ -> the json response from the api
        #   - +factory+ -> the model factory responsible for building the models
        #
        # * *Returns* :
        #   - Array containing instances of AboutYou::SDK::Model::Category
        ###
        def self.parse_categories(json_object, factory)
          return NOT_REQUESTED unless json_object.key?('categories')
          return [] if json_object['categories'].nil?

          categories = []
          json_object['categories'].each do |category|
            categories.push(factory.create_category(category))
          end

          categories
        end

        ###
        # This method parses the json object and builds product models from it
        #
        # * *Args*    :
        #   - +json_object+ -> the json response from the api
        #   - +factory+ -> the model factory responsible for building the models
        #
        # * *Returns* :
        #   - Array containing instances of AboutYou::SDK::Model::Product
        ###
        def self.parse_products(json_object, factory)
          return NOT_REQUESTED unless json_object.key?('products')
          return [] if json_object['products'].nil?

          products = []
          json_object['products'].each do |product|
            products.push(factory.create_product(product))
          end

          products
        end
        
        ###
        # This method parses the json object and builds product models from it
        #
        # * *Args*    :
        #   - +json_object+ -> the json response from the api
        #   - +factory+ -> the model factory responsible for building the models
        #
        # * *Returns* :
        #   - Array containing instances of AboutYou::SDK::Model::Product
        ###
        def self.parse_brands(json_object, factory)
          return NOT_REQUESTED unless json_object.key?('brands')
          return [] if json_object['brands'].nil?

          brands = []
          json_object['brands'].each do |brand|
              brands.push(factory.create_brand(brand))
          end

          brands
        end
      end
    end
  end
end
